package com.jyw.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyw.system.domain.SysDeptIpAddress;
import com.jyw.system.domain.dto.SysDeptIpAddressBatchAddDto;
import com.jyw.system.domain.dto.SysDeptIpAddressEditDto;
import com.jyw.system.domain.dto.SysDeptIpAddressPageDto;
import com.jyw.system.domain.dto.SysDeptIpAddressSubAddDto;
import com.jyw.system.domain.vo.SysDeptIpAddressVo;
import com.jyw.system.mapper.SysDeptIpAddressMapper;
import com.jyw.system.service.ISysDeptIpAddressService;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 科室IPService业务层处理
 *
 * @author ruoyi
 * @date 2024-01-10
 */
@Service
public class SysDeptIpAddressServiceImpl extends ServiceImpl<SysDeptIpAddressMapper, SysDeptIpAddress> implements ISysDeptIpAddressService {
    @Resource
    private SysDeptIpAddressMapper sysDeptIpAddressMapper;

    @Override
    public List<SysDeptIpAddressVo> getIpAddressList(SysDeptIpAddressPageDto pageDto) {
        List<SysDeptIpAddressVo> list = sysDeptIpAddressMapper.getIpAddressList(pageDto);
        return ConversionUtil.conversionList(list, SysDeptIpAddressVo.class);
    }

    @Override
    public List<SysDeptIpAddressVo> getIpAddressByDeptId(Long deptId) {
        List<SysDeptIpAddressVo> list = sysDeptIpAddressMapper.getIpAddressByDeptId(deptId);
        return ConversionUtil.conversionList(list, SysDeptIpAddressVo.class);
    }

    @Override
    public SysDeptIpAddressVo getInfoById(Long id) {
        SysDeptIpAddress sysDeptIpAddress=this.getById(id);
        if(sysDeptIpAddress!=null){
            SysDeptIpAddressVo ipAddressVo=BeanUtil.toBean(sysDeptIpAddress,SysDeptIpAddressVo.class);
            return ConversionUtil.conversion(ipAddressVo,SysDeptIpAddressVo.class);
        }
        return null;
    }

    @Override
    public SysDeptIpAddress getIpAddress(Long deptId, String ipAddress) {
        QueryWrapper<SysDeptIpAddress> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDeptIpAddress::getDeptId,deptId);
        queryWrapper.lambda().eq(SysDeptIpAddress::getIpAddress,ipAddress);
        queryWrapper.last("LIMIT 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public String joinIpAddress(String ipAddressId) {
        if(StrUtil.isNotEmpty(ipAddressId)){
            List<Long> ipIdList= JSON.parseArray(ipAddressId,Long.class);
            List<SysDeptIpAddress> ipAddressList=this.lambdaQuery().in(SysDeptIpAddress::getId,ipIdList).list();
            if(CollUtil.isNotEmpty(ipAddressList)){
                String result = ipAddressList.stream().map(SysDeptIpAddress::getIpAddress)
                        .collect(Collectors.joining(", "));
                return result;
            }
        }
        return null;
    }

    @Override
    public void sysBatchAdd(SysDeptIpAddressBatchAddDto batchAddDto) {
        if (CollUtil.isNotEmpty(batchAddDto.getSubList())) {
            Long deptId = batchAddDto.getDeptId();
            List<SysDeptIpAddress> phoneBooks = new ArrayList<>();
            List<SysDeptIpAddressSubAddDto> subList = batchAddDto.getSubList();
            subList.forEach(sub -> {
                SysDeptIpAddress phoneBook = new SysDeptIpAddress();
                phoneBook.setDeptId(deptId);
                phoneBook.setIpAddress(sub.getIpAddress());
                phoneBook.setCreateTime(LocalDateTime.now());
                phoneBooks.add(phoneBook);
            });
            this.saveBatch(phoneBooks);
        }
    }

    @Override
    public void sysEdit(SysDeptIpAddressEditDto editDto) {
        SysDeptIpAddress deptIpAddress=this.getById(editDto.getId());
        if(deptIpAddress!=null){
            BeanUtil.copyProperties(editDto, deptIpAddress);
            deptIpAddress.setUpdateTime(LocalDateTime.now());
            this.updateById(deptIpAddress);
        }
    }

    /**
     * 批量删除科室IP
     *
     * @param ids 需要删除的科室IP主键
     * @return 结果
     */
    @Override
    public int deleteSysDeptIpAddressByIds(Long[] ids) {
        return sysDeptIpAddressMapper.deleteSysDeptIpAddressByIds(ids);
    }

}
